package org.szgb.console.nlms.iams.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.szgb.console.nlms.iams.service.NlmsIamsService;
import org.szgb.console.utils.TokenUtil;

import java.io.IOException;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping(value = "/nlms/iams")
@Tag(name = "新线筹备系统", description = "新线筹备系统管理模块")
public class NlmsIamsController {
    private final NlmsIamsService nlmsIamsService;

    public NlmsIamsController(NlmsIamsService nlmsIamsService) {
        this.nlmsIamsService = nlmsIamsService;
    }

    @Operation(summary = "获取新建问题表单", description = "获取新建问题表单。")
    @PostMapping("/getAddProblemForm")
    public String getAddProblemForm(HttpServletRequest request, HttpServletResponse response) throws InterruptedException, IOException {
        String addProblemForm = nlmsIamsService.getAddProblemForm(TokenUtil.getUid(request));
        if(Objects.equals(addProblemForm, "{\"msg\":null,\"success\":false}")){
            nlmsIamsService.loginIams(TokenUtil.getUid(request));
            addProblemForm = nlmsIamsService.getAddProblemForm(TokenUtil.getUid(request));
        }
        return addProblemForm.split("</body>")[0].split("<body>")[1];
    }

    @Operation(summary = "获取位置列表", description = "获取位置列表。")
    @PostMapping("/getLocation")
    public String getLocation(@RequestParam String stationType, HttpServletRequest request, HttpServletResponse response) throws InterruptedException, IOException {
        String location = nlmsIamsService.getLocation(TokenUtil.getUid(request), stationType);
        if(Objects.equals(location, "{\"msg\":null,\"success\":false}")){
            nlmsIamsService.loginIams(TokenUtil.getUid(request));
            location = nlmsIamsService.getLocation(TokenUtil.getUid(request), stationType);
        }
        return location;
    }

    @Operation(summary = "获取专业列表", description = "获取专业列表。")
    @PostMapping("/getProfession")
    public String getProfession(HttpServletRequest request, HttpServletResponse response) throws InterruptedException, IOException {
        String profession = nlmsIamsService.getProfession(TokenUtil.getUid(request));
        if(Objects.equals(profession, "{\"msg\":null,\"success\":false}")){
            nlmsIamsService.loginIams(TokenUtil.getUid(request));
            profession = nlmsIamsService.getProfession(TokenUtil.getUid(request));
        }
        return profession;
    }

    @Operation(summary = "获取问题分类", description = "获取问题分类。")
    @PostMapping("/getClass")
    public String getClass(@RequestParam String problemClassify, HttpServletRequest request, HttpServletResponse response) throws InterruptedException, IOException {
        String aClass = nlmsIamsService.getClass(TokenUtil.getUid(request), problemClassify);
        if(Objects.equals(aClass, "{\"msg\":null,\"success\":false}")){
            nlmsIamsService.loginIams(TokenUtil.getUid(request));
            aClass = nlmsIamsService.getClass(TokenUtil.getUid(request), problemClassify);
        }
        return aClass;
    }

    @Operation(summary = "获取责任单位", description = "获取责任单位。")
    @PostMapping("/getSecurity")
    public String getSecurity( HttpServletRequest request, HttpServletResponse response) throws InterruptedException, IOException {
        String aClass = nlmsIamsService.getSecurity(TokenUtil.getUid(request));
        if(Objects.equals(aClass, "{\"msg\":null,\"success\":false}")){
            nlmsIamsService.loginIams(TokenUtil.getUid(request));
            aClass = nlmsIamsService.getSecurity(TokenUtil.getUid(request));
        }
        return aClass;
    }
}
